﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace eCompany.Forms
{
    public partial class currencies_form : Form
    {
        public eCompany.eObj.currency_list m_cl = null;

        public currencies_form(ref eCompany.eObj.currency_list cl)
        {
            InitializeComponent();
            this.Icon = pics.eCompanyIcon;
            m_cl = cl;
            RefreshCurrencyList();
        }

        private void RefreshCurrencyList()
        {
            currencieslistView.Items.Clear();
            foreach (eCompany.eObj.currency c in m_cl.currencies)
            {
                eCompany.eManager.eManager eM = new eCompany.eManager.eManager();
                ListViewItem item = new ListViewItem();
                item.Text = c.short_name;
                eCompany.eObj.currency_value cv = eM.getLastCurrencyValue(c);
                item.SubItems.Add(cv.value.ToString());
                item.SubItems.Add(cv.date.ToString());

                currencieslistView.Items.Add(item);
            }
        }

        private void refreshRateCmd_Click(object sender, EventArgs e)
        {
            /*this.Cursor = Cursors.WaitCursor;
            refreshProgressBar.Visible = true;
            refreshProgressBar.Enabled = true;
            eCompany.eManager.eManager eM = new eManager.eManager();
            eM.refreshCurrencies(ref m_cl, refreshPG);
            RefreshCurrencyList();
            refreshProgressBar.Enabled = false;
            refreshProgressBar.Visible = false;
            this.Cursor = Cursors.Default;*/

            /*this.Cursor = Cursors.WaitCursor;
            refreshProgressBar.Visible = true;
            refreshProgressBar.Enabled = true;*/
            startRefreshDelegate = new startRefreshHandler(startRefresh);
            stopRefreshDelegate = new stopRefreshHandler(stopRefresh);
            refreshCurrentCurrencyDelegate = new refreshCurrentCurrencyHandler(refreshCurrentCurrency);
            m_RefreshRateThread = new Thread(refreshRateProc);
            m_RefreshRateThread.Start(m_cl);
            /*RefreshCurrencyList();
            refreshProgressBar.Enabled = false;
            refreshProgressBar.Visible = false;
            this.Cursor = Cursors.Default;*/
        }

        private delegate void startRefreshHandler();
        private startRefreshHandler startRefreshDelegate;
        private delegate void stopRefreshHandler();
        private stopRefreshHandler stopRefreshDelegate;
        private delegate void refreshCurrentCurrencyHandler(string short_currency);
        private refreshCurrentCurrencyHandler refreshCurrentCurrencyDelegate;
        Thread m_RefreshRateThread = null;

        private void refreshRateProc(object o)
        {
            this.Invoke(this.startRefreshDelegate);
            eCompany.eObj.currency_list cl = (eCompany.eObj.currency_list)o;
            eCompany.eManager.eManager eM = new eManager.eManager();
            eM.refreshCurrencies(ref cl, this.refreshPG);
            this.Invoke(this.stopRefreshDelegate);
        }

        public void refreshPG(string short_currency)
        {
            this.Invoke(this.refreshCurrentCurrencyDelegate, new object[] {short_currency});
        }

        private void refreshCurrentCurrency(string short_currency)
        {
            currencyLbl.Text = short_currency;
        }

        [PreEmptive.Attributes.Feature("Currency.Refresh")]
        private void startRefresh()
        {
            this.Cursor = Cursors.WaitCursor;
            this.closeCmd.Visible = false;
            this.refreshRateCmd.Visible = false;
            this.refreshProgressBar.Visible = true;
            this.refreshProgressBar.Enabled = true;
            this.currencyLbl.Visible = true;
            
        }

        private void stopRefresh()
        {
            this.RefreshCurrencyList();
            this.refreshProgressBar.Enabled = false;
            this.refreshProgressBar.Visible = false;
            this.refreshRateCmd.Visible = true;
            this.closeCmd.Visible = true;
            this.currencyLbl.Visible = false;
            this.Cursor = Cursors.Default;
        }

        private void currencies_form_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (m_RefreshRateThread != null && m_RefreshRateThread.ThreadState == ThreadState.Running)
                e.Cancel=true;
        }

        private void closeCmd_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
